<?php
namespace Common\Controller;
use Think\Controller;

class BaseController extends Controller {

    public static $zxyear = array("2", "3", "5");//3年, 5年
    public static $shebao = array("是", "否");
    public static $gender = array("男", "女");
    public static $dibao = array("非", "是");
    public static $relation = array("父亲", "母亲", "配偶", "儿子", "女儿", "其他");
    public static $marriage = array("未婚", "已婚", "离婚", "丧偶");
    public static $house = array("自有产权房", "无自有产权房");
    public static $blacklistReason = array("自动放弃", "恶意放弃", "资料虚假", "重复申请", "恶意欠租" ,"恶意损坏财产", "其他原因");
    public static $attr = array("低保", "残疾", "军烈", "孤老", "单亲", "其他");
    public static $hujiqk = array("家庭成员身份证", "家庭成员户口薄", "结婚证", "离婚证及离婚协议或法院判决书");
    public static $workState = array("机关事业单位", "企业", "个体", "灵活就业", "企业改制下岗退休");
    public static $houseState = array("租私房", "单位公房", "直管公房", "其他");
    public static $applierType = array("外来务工人员", "新就业无房职工", "农村低保对象",
        "城镇企改下岗退休家庭", "农村精准扶贫建档立卡对象", "政府引进特殊人才");
    /*public static $applierType = array("城镇中等偏下收入住房困难家庭", "城市低收入住房困难家庭", "新就业无房职工",
        "外来务工人员", "市级以上劳模、见义勇为人员", "政府引进特殊人才", "重点优抚对象", "重大疾病求助对象");*/

    // 当前登陆用户基本信息
    public static $u = array();

    protected function _initialize()
    {
        $this->assign('controller', CONTROLLER_NAME);
        $this->assign('model', MODEL_NAME);
        $this->assign('action', ACTION_NAME);
        $this->assign('site_url', C("site_url"));

        //登陆用户信息
        $where = array("username"=>session("username"));
        $user = M("user u")
            ->field("u.*, r.name as role_name, c.area_id, r.code as role_code, c.name as company_name")
            ->where($where)
            ->join(C('DB_PREFIX')."role r on r.id=u.role_id", "LEFT")
            ->join(C('DB_PREFIX')."company c on c.id=u.company_id", "LEFT")
            ->find();
        $this->assign('u', $user);
        self::$u = $user;

        //登陆用户未读消息数量
        $where = array("state"=>"未读","ruser"=>session("username"));
        $count = M("notice")->where($where)->count();
        $this->assign("unreadNotice", $count);

        //权限代码
        $rightsCode = M("rassign a")
            ->field("r.code")
            ->where(array('a.role_id'=>$user['role_id']))
            ->join(C('DB_PREFIX')."rights r on r.id=a.rights_id", "LEFT")
            ->select();
        $code = array();
        foreach ($rightsCode as $item) {
            $code[] = $item['code'];
        }
        $this->assign('rightsCode', $code);
    }

    public static function IsDateValid($date, $format = "Y/m/d") {
        $unixTime = strtotime($date);
        if (!$unixTime) { //strtotime转换不对，日期格式显然不对。
            return false;
        }
        //校验日期的有效性，只要满足其中一个格式就OK
        if (date($format, $unixTime) == $date) {
            return true;
        }
        return false;
    }

}